package soen343.domain;

import java.sql.ResultSet;
import java.sql.SQLException;

import soen343.data.QuestionTDG;

public class QuestionMapper {
	private static final int SHORT_ANSWER = 1;
	private static final int NUMERIC = 2;
	private static final int MULTIPLE_CHOICE = 3;
	
	private QuestionMapper() {}

	public static Question findById(int id) {
		Question question = null;
		
		try {
			ResultSet rs = QuestionTDG.findById(id);
			rs.first();
			
			switch( rs.getInt("type") ) {
				case SHORT_ANSWER:
					question = createShortAnswerQuestion(rs);
					break;
				case NUMERIC:
					question = createNumericQuestion(rs);
					break;
				case MULTIPLE_CHOICE:
					question = createMultipleChoiceQuestion(rs);
					break;
			}
		} catch(SQLException e) {
			// just return the null object
		}
		
		return question;
	}

	private static Question createMultipleChoiceQuestion(ResultSet rs) throws SQLException {
		rs.first();
		MultipleChoiceQuestion question = new MultipleChoiceQuestion(rs.getString("name"));
		question.setText(rs.getString("text"));
		question.addChoice(rs.getString("choice_text"));
		while(rs.next()) {
			question.addChoice(rs.getString("choice_text"));
		}
		return question;
	}

	private static Question createNumericQuestion(ResultSet rs) throws SQLException {
		rs.first();
		NumericQuestion question = new NumericQuestion(rs.getString("name"));
		question.setText(rs.getString("text"));
		question.setBounds(
				rs.getInt("upperbound"),
				rs.getInt("lowerbound"));
		return question;
	}

	private static Question createShortAnswerQuestion(ResultSet rs) throws SQLException {
		rs.first();
		ShortAnswerQuestion question = new ShortAnswerQuestion(rs.getString("name"));
		question.setText(rs.getString("text"));
		// set answer ???
		return question;
	}
}
